package com.gun.service;

/**
 * @Authar: 吾图南
 * @Data: 2022/12/10 19:03
 * @Package: com.gun.service
 * @ClassName: CommentService
 * @Description: TODO
 */
import com.gun.domain.Comment;
import com.gun.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Optional;

@Service
public class CommentService  {

    @Autowired
    private CommentRepository commentRepository;

    // 根据评论id查询评论信息

    @Cacheable(cacheNames = "comment",unless = "#result==null")
    //@Cacheable(cacheNames = "comment")
    public Comment findById(int comment_id){
        Optional<Comment> optional = commentRepository.findById(comment_id);
        if(optional.isPresent()){
            return optional.get();
        }
        return null;
    }


    // 更新评论信息
    @CachePut(cacheNames = "comment",key = "#result.id")
    public Comment updateComment(Comment comment){
        commentRepository.updateComment(comment.getAuthor(), comment.getaId());
        return comment;
    }

    // 删除评论信息
    @CacheEvict(cacheNames = "comment")
    public void deleteComment(int comment_id){
        commentRepository.deleteById(comment_id);
    }
}


